/**********************************************************************/
#include <QMessageBox>
#include <QInputDialog>
/**********************************************************************/
#include "mainframe.h"
#include "ui_mainframe.h"

/**********************************************************************/
MainFrame::MainFrame(QWidget *parent)
  : QMainWindow(parent), ui(new Ui::MainFrame)
{
  ui->setupUi(this);
  port = DEFAULT_PORT;
}

/**********************************************************************/
void MainFrame::displayError(QString msg)
{
  on_menuServerStop_triggered();
  QMessageBox::information(this, "Error", msg);
}

/**********************************************************************/
void MainFrame::printProtocolMessage(QString msg)
{
  ui->txtProtocol->appendPlainText(msg);
}

/**********************************************************************/
void MainFrame::displayStatusMessage(QString msg)
{
  ui->statusBar->showMessage(msg, DEFAULT_STATUSBAR_TIMEOUT);
}

/**********************************************************************/
void MainFrame::addUser(QString user)
{
  ui->txtUsers->appendPlainText(user);
}

/**********************************************************************/
void MainFrame::on_menuServerStart_triggered()
{
  ui->menuServerStop->setEnabled(true);
  ui->menuServerStart->setEnabled(false);
  ui->menuServerSettings->setEnabled(false);
  emit startServer(port);
}

/**********************************************************************/
void MainFrame::on_menuServerStop_triggered()
{
  ui->menuServerStop->setEnabled(false);
  ui->menuServerStart->setEnabled(true);
  ui->menuServerSettings->setEnabled(true);
  emit stopServer();
}

/**********************************************************************/
void MainFrame::on_menuServerSettings_triggered()
{
  bool ok;
  quint16 port = (quint16) QInputDialog::getInt(
      this, "Port Setting", "Port", this->port, 1025, 65535, 1, &ok);

  if(ok && this->port != port)
  {
    displayStatusMessage(QString("Changed server port from %1 to %2.")
                         .arg(this->port).arg(port));
    this->port = port;
  }
}

/**********************************************************************/
void MainFrame::on_menuFileExit_triggered()
{
  emit close();
}

/**********************************************************************/
void MainFrame::on_menuHelpAbout_triggered()
{
  QMessageBox::about(this, PROGRAM_NAME,
                     ABOUT_TEXT);
}

void MainFrame::on_menuHelpAboutQT_triggered()
{
  QMessageBox::aboutQt(this, PROGRAM_NAME);
}
